<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/11/24 0024
  Time: 9:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户录入</title>
    <jsp:include page="../template/bootstrap_common.jsp"></jsp:include>
</head>
<body>
    <div class="row1">
    <%-- 工具栏--%>
    <ul class="breadcrumb">
        <li><a href="${pageContext.request.contextPath}/wellcome.jsp">主页</a></li>
        <li>用户录入</li>
    </ul>
        <br/>
        <br/>
    <div>
        <div style="margin: 0 auto; width: 400px;height: 500px;">
            <div>
                <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                    <input type="text" class="form-control" placeholder="请输入账号" id="account" autocomplete="off" name="account">
                </div>
                <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                    <input type="text" class="form-control" placeholder="用户名" autocomplete="off" id="username" name="username">
                </div>
                <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                    <input type="password" class="form-control" placeholder="请输入密码" autocomplete="off" id="password" name="password">
                </div>
            </div>
            <div>
                <div class="form-group" style="margin-left: 20px;margin-right: 20px;">
                    <button type="button" id="btnSubmit" class="btn btn-primary form-control">注册</button>
                </div>
                <div id="message" class="form-group" style="margin-left: 20px;margin-right: 20px;">

                </div>
            </div>
        </div>
    </div>
</div>
    <script>
        $(function () {

            $("#btnSubmit").click(saveUser)

        });

        function saveUser() {
            //获取输入框对象
            var account1 = $("#account").val();
            var username1 = $("#username").val();
            var password1 = $("#password").val();

            var obj = {};
            obj.account = account1;
            obj.userName = username1;
            obj.password = password1;
            // alert(JSON.stringify(obj))
            $.ajax({
                url:"${pageContext.request.contextPath}/user/saveUser",
                type:"POST",
                contentType:"application/json",//传递给前端数据的类型
                data:JSON.stringify(obj),//json数据
                dataType:'json',//后台响应给前台的数据类型
                success:function (resp) {
                    $("#message").html("");
                    if(resp.message>0){
                        var msg = "注册成功！！";
                        console.log("成功")
                        var message = $("#message");
                        var mes = $("<div>");
                        mes.html('<div class="alert alert-success alert-dismissible" role="alert">\n' +
                            '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                            '  注册成功！！！\n' +
                            '</div>')
                        message.append(mes);
                    }else{
                        var msg = "账号重复 ， 请重新输入！！！";
                        console.log("失败")
                        var message = $("#message");
                        var mes = $("<div>");
                        mes.html('<div class="alert alert-danger alert-dismissible" role="alert">\n' +
                            '  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
                            '  用户名重复 ， 请重新输入！！！\n' +
                            '</div>')
                        message.append(mes);
                    }
                }
            })
        }
    </script>
</body>
</html>
